package com.free.semantic.core.subconcept.impl;

import com.alibaba.fastjson.JSONObject;
import com.free.semantic.common.*;
import com.free.semantic.core.ConceptInstanceManage;
import com.free.semantic.core.ConceptManage;
import com.free.semantic.core.impl.ConceptInstanceManageImpl;
import com.free.semantic.core.script.impl.ScriptEngineImpl;
import com.free.semantic.core.subconcept.SubConceptInstanceProvider;
import com.free.semantic.req.RelationDefineGroup;

import java.util.List;

import static com.free.semantic.core.utils.getWeather;
import static com.free.semantic.utils.Utils.*;

public class WeatherProviderImpl implements SubConceptInstanceProvider {


    @Override
    public String getName() {
        return "天气模型";
    }

    @Override
    public ConceptInstanceManage getInstanceMange() {
        RelationDefineGroup group1 = new RelationDefineGroup();
        // relation 包含(A,B)
        defineRelation(group1, "找到", defineItem(oneObj("A"), oneObj("B"))); // B->A=1 //代表 针对任意一个B，都可以找到 1个A与之对应

        ConceptManage model1 = getConceptManage(group1);
        object(model1, "城市", "天气综合信息", "经纬坐标", "天气", "温度", "绝对温度", "体感温度", "最大温度", "海拔", "能见度");

        createRelation(model1, "找到", "城市", "天气综合信息");
        createRelation(model1, "找到", "天气综合信息", "经纬坐标");
        createRelation(model1, "找到", "天气综合信息", "天气");
        createRelation(model1, "找到", "天气综合信息", "温度");
        createRelation(model1, "找到", "天气综合信息", "体感温度");
        createRelation(model1, "找到", "天气综合信息", "最大温度");

        createRelation(model1, "找到", "体感温度", "绝对温度");
        createRelation(model1, "找到", "最大温度", "绝对温度");
        createRelation(model1, "找到", "温度", "绝对温度");

        createRelation(model1, "找到", "天气综合信息", "海拔");
        createRelation(model1, "找到", "天气综合信息", "能见度");

        // 创建模型的一个实现，并且实现 省份查询城市 和 城市查询 省份 函数（添加一些测试数据）
        ConceptInstanceManage modelInstance = new ConceptInstanceManageImpl(model1);

        registerTheSameFunction(modelInstance, "体感温度", "绝对温度");
        registerTheSameFunction(modelInstance, "最大温度", "绝对温度");
        registerTheSameFunction(modelInstance, "温度", "绝对温度");

        modelInstance.registerFunction(Ls.of("城市"), "天气综合信息", context -> {
            List<String> value = context.getListValue("城市");
            String name = value.get(0);
            String weather = getWeather(name);
            JSONObject jsonObject = JSONObject.parseObject(weather);
            Integer code = jsonObject.getInteger("cod");
            if (!code.equals(200)) {
                throw new RuntimeException(jsonObject.getString("message"));
            }
            context.setValue("天气综合信息", jsonObject);
        });

        modelInstance.registerFunction(Ls.of("天气综合信息"), "经纬坐标", context -> {
            List<JSONObject> value = context.getListValue("天气综合信息");
            JSONObject weather = value.get(0);
            context.setValue("经纬坐标", weather.getJSONObject("coord"));
        });

        modelInstance.registerFunction(Ls.of("天气综合信息"), "天气", context -> {
            List<JSONObject> value = context.getListValue("天气综合信息");
            JSONObject weather = value.get(0);
            String string = ((JSONObject) weather.getJSONArray("weather").get(0)).getString("main");
            context.setValue("天气", string);
        });

        modelInstance.registerFunction(Ls.of("天气综合信息"), "温度", context -> {
            List<JSONObject> value = context.getListValue("天气综合信息");
            JSONObject weather = value.get(0);
            JSONObject main = weather.getJSONObject("main");
            context.setValue("温度", main.getFloatValue("temp"));
        });

        modelInstance.registerFunction(Ls.of("天气综合信息"), "体感温度", context -> {
            List<JSONObject> value = context.getListValue("天气综合信息");
            JSONObject weather = value.get(0);
            JSONObject main = weather.getJSONObject("main");
            context.setValue("体感温度", main.getFloatValue("feels_like"));
        });


        modelInstance.registerFunction(Ls.of("天气综合信息"), "最大温度", context -> {
            List<JSONObject> value = context.getListValue("天气综合信息");
            JSONObject weather = value.get(0);
            JSONObject main = weather.getJSONObject("main");
            context.setValue("最大温度", main.getFloatValue("temp_max"));
        });

        modelInstance.registerFunction(Ls.of("天气综合信息"), "海拔", context -> {
            List<JSONObject> value = context.getListValue("天气综合信息");
            JSONObject weather = value.get(0);
            JSONObject main = weather.getJSONObject("main");
            context.setValue("海拔", main.getFloatValue("sea_level"));
        });

        modelInstance.registerFunction(Ls.of("天气综合信息"), "能见度", context -> {
            List<JSONObject> value = context.getListValue("天气综合信息");
            JSONObject weather = value.get(0);
            context.setValue("能见度", weather.getJSONObject("visibility"));
        });

        new ScriptEngineImpl().execScript(modelInstance, "hideObject(天气综合信息)");
        return modelInstance;
    }
}
